package com.jackrain.clouddata.oms.services;/**
 * Created by Administrator on 2017/3/28.
 */

import com.alibaba.dubbo.common.utils.NetUtils;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.aliyun.openservices.ons.api.Message;
import com.aliyun.openservices.ons.api.SendResult;
import com.aliyun.openservices.ons.api.bean.ProducerBean;
import com.aliyun.openservices.ons.api.exception.ONSClientException;
import com.aliyun.openservices.ons.api.impl.util.MsgConvertUtil;
import com.jackrain.clouddata.oms.dao.LogStageDao;
import com.jackrain.clouddata.oms.util.ExceptionUtil;
import com.jackrain.nea.config.PropertiesConf;
import com.jackrain.nea.util.ApplicationContextHandle;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.UUID;

/***
 * 生产消息
 */

@Service
@Slf4j
public class SendMqServies {

    @Autowired
    private PropertiesConf pconf;

    @Autowired
    private ProducerBean producerBean;

    @Autowired
    private SendSMSServies smsServies;

    /**
     * 发送消息
     * @param dataarry
     * @param ctag
     * @throws Exception
     */
    public void  sendMqinfo(JSONArray dataarry,String topic, String ctag, Long id) throws Exception {

        Message msg = null;
        //  Expr 就是 tag
        msg = new Message(topic, ctag, MsgConvertUtil
                .objectSerialize(dataarry));
        String str = UUID.randomUUID().toString();
        msg.setKey(str);
        try {
            SendResult sendResult = producerBean.send(msg);
            assert sendResult != null;
            log.debug("send success: " + sendResult.getMessageId()+topic);
            LogStageDao.insertLoginformq(str, dataarry.size(),"success", topic, ctag,id );
        } catch (ONSClientException e) {
            LogStageDao.insertLoginformq("发送失败", dataarry.size(),e.getMessage().toString(), topic, ctag,id );
            // 入库错误日志
            LogStageDao logstage = ApplicationContextHandle.getBean(LogStageDao.class);
            String errmsg = ExceptionUtil.getExceptionAllinformation(e);
            String server_ip = NetUtils.getLocalAddress().getHostAddress();
            logstage.addServerErrLog(topic, ctag, errmsg,server_ip);
            log.error("发送失败");
        }

    }


    /**
     * 发送消息
     * @param dataobject
     * @param topic
     * @param ctag
     * @throws Exception
     */
    public void sendMqinfoObj(JSONObject dataobject, String topic, String ctag) throws Exception {


        if(topic.startsWith("ons.loginfor.err")){
            smsServies.sendSMS(dataobject);
        }

        Message msg = null;
        msg = new Message(topic, ctag, MsgConvertUtil
                .objectSerialize(dataobject));
        String str = UUID.randomUUID().toString();
        msg.setKey(str);
        SendResult sendResult = producerBean.send(msg);
        assert sendResult != null;
        log.debug("send success: " + str + topic);
    }

    /**
     * 发送多条数据消息
     * @param dataarry
     * @param ctag
     * @throws Exception
     */
    public  void  sendMqinfoArr(JSONArray dataarry,String topic, String ctag ) throws Exception {

        Message msg = null;
        //  Expr 就是 tag
        msg = new Message(topic, ctag, MsgConvertUtil
                .objectSerialize(dataarry));
        String str = UUID.randomUUID().toString();
        msg.setKey(str);
        SendResult sendResult = producerBean.send(msg);
        assert sendResult != null;
        log.debug("send success: " + str + topic);
    }



}
